package com.cjk.console.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.cjk.console.model.ZTree;
import com.cjk.console.request.RoleRequest;
import com.cjk.foundation.commons.BusinessException;
import com.cjk.foundation.commons.response.PageResponse;
import com.cjk.foundation.dao.EmployeeDao;
import com.cjk.foundation.dao.MenuDao;
import com.cjk.foundation.dao.RoleDao;
import com.cjk.foundation.entity.EmployeeEntity;
import com.cjk.foundation.entity.RoleEntity;
import com.cjk.foundation.enums.IsStatus;
import com.cjk.foundation.util.PasswordUtil;

@Service
public class AuthServiceImpl implements AuthService {

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private MenuDao menuDao;

	@Autowired
	private EmployeeDao employeeDao;

	@Override
	public PageResponse getRoleList(Integer page, Integer pageSize) {
		List<RoleEntity> roleEntities = roleDao.selectListByCondition(page, pageSize);
		if (CollectionUtils.isEmpty(roleEntities)) {
			return PageResponse.success(0, pageSize, new ArrayList<>());
		}
		Integer count = roleDao.selectCountByCondition();
		return PageResponse.success(count, pageSize, roleEntities);
	}

	@Override
	public PageResponse getEmployeeList(Integer page, Integer pageSize) {
		List<EmployeeEntity> employeeEntities = employeeDao.selectListByCondition(page, pageSize);
		if (CollectionUtils.isEmpty(employeeEntities)) {
			return PageResponse.success(0, pageSize, new ArrayList<>());
		}
		Integer count = employeeDao.selectCountByCondition();
		return PageResponse.success(count, pageSize, employeeEntities);
	}

	@Transactional
	@Override
	public void saveRole(RoleRequest request) {
		Integer count = roleDao.checkExist(request.getRoleName(), request.getId());
		if (count != 0) {
			throw new BusinessException("已存在相同的角色名");
		}
		if (request.getId() == null) {
			RoleEntity roleEntity = new RoleEntity();
			roleEntity.setName(request.getRoleName());
			roleDao.insert(roleEntity);
			for (ZTree zTree : request.getzTrees()) {
				IsStatus isStatus = zTree.isChecked() ? IsStatus.TRUE : IsStatus.FALSE;
				menuDao.insertMenuAuth(zTree.getId(), roleEntity.getId(), isStatus);
			}
		} else {
			roleDao.update(request.getRoleName(), request.getId());
			for (ZTree zTree : request.getzTrees()) {
				IsStatus isStatus = zTree.isChecked() ? IsStatus.TRUE : IsStatus.FALSE;
				menuDao.updateMenuAuth(zTree.getId(), request.getId(), isStatus);
			}
		}
	}

	@Transactional
	@Override
	public void deleteRole(Integer id) {
		roleDao.deleteById(id);
		menuDao.deleteMenuAuthByRoleId(id);
	}

	@Transactional
	@Override
	public void saveEmployee(Integer id, String username, String password, Integer roleId) {
		if (id == null) {
			EmployeeEntity employeeEntity = new EmployeeEntity();
			employeeEntity.setPassword(PasswordUtil.hexStringToBytes(password));
			employeeEntity.setRoleId(roleId);
			employeeEntity.setUsername(username);
			employeeDao.insert(employeeEntity);
		} else {
			byte[] pwdByte = null;
			if (!StringUtils.isEmpty(password)) {
				pwdByte = PasswordUtil.hexStringToBytes(password);
			}
			employeeDao.update(id, pwdByte, roleId);
		}
	}
}
